package com.qizhan100.qtools.web;

import javax.servlet.http.HttpServletRequest;

public class DomainUtil {
    /**
     * @param request
     * @return
     * @author:zhuanglt
     * @email:longteng1988@qq.com
     * @创建日期:2016-7-18
     * @功能说明：
     */
    public static String getBasePath(HttpServletRequest request) {
        StringBuffer url = request.getRequestURL();
        url.delete(url.length() - request.getRequestURI().length(), url.length());
        return url.toString();
    }
    
    /**
     * @param request
     * @return
     * @author:zhuanglt
     * @email:longteng1988@qq.com
     * @创建日期:2016-1-5
     * @功能说明：返回请求域名
     */
    public static String getDomain(HttpServletRequest request) {
        StringBuffer url = request.getRequestURL();
        url.delete(url.length() - request.getRequestURI().length(), url.length());
        if (url.toString().startsWith("http://www.")) {
            url.delete(0, 11);
        } else {
            url.delete(0, 7);
        }
        return url.toString();
    }
    
    /**
     * @param domain
     * @return
     * @author:zhuanglt
     * @email:longteng1988@qq.com
     * @创建日期:2016-8-23
     * @功能说明：是否为www开头域名
     */
    public static boolean iswww(String domain) {
        return domain.startsWith("www.");
    }
}
